package edu.hawaii.wattwaiter.page.stoplight;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.wattdepot.util.tstamp.Tstamp;
import edu.hawaii.wattwaiter.wattdepotcommands.CarbonFlagSetter;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import edu.hawaii.wattwaiter.page.WattWaiterSession;

/**
 * Displays stop light corresponding to current hour's carbon intensity.
 * 
 * @author Kevin Chiogioji
 * @author David Mau
 */

public class StopLight extends Panel {
  private static final long serialVersionUID = 1L;

  /**
   * Implements stop light display.
   * 
   */
  public StopLight() {

    super(TabbedPanel.TAB_PANEL_ID);
    WattWaiterSession session = (WattWaiterSession) getSession();
    session.setFirstVisit(true);

    String debugDateTime = System.getProperty("testing.curr.time");

    List<Integer> flagColors;
    Calendar calendar = new GregorianCalendar();
    int currentYear = calendar.get(Calendar.YEAR);
    int currentMonth = calendar.get(Calendar.MONTH) + 1;
    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
    String currentDate;

    Calendar currentTime = Calendar.getInstance();

    int color = 0;

    

    int sessionHour = session.getLastHour();
    //System.out.println("DEBUG hour: " + sessionHour);

    int sessionColor = session.getCurrentColor();
    //System.out.println("DEBUG color: " + sessionColor);

    // Get current hour of the day
    int currentHour = currentTime.get(Calendar.HOUR_OF_DAY);

    //System.out.println("DEBUG: current hour: " + currentHour);

    if (sessionHour == currentHour) {
      color = sessionColor;
      //System.out.println("DEBUG Used session variable.");

    }

    else {

      // Format current date if necessary
      if (currentDay < 10) {
        currentDate = currentYear + "-" + currentMonth + "-" + "0" + currentDay;
      }
      else {
        currentDate = currentYear + "-" + currentMonth + "-" + currentDay;
      }

      // If the system property is set,
      if (debugDateTime != null) {
        XMLGregorianCalendar convertedDateTime = null;

        // Convert the string into a time stamp.
        try {
          convertedDateTime = Tstamp.makeTimestamp(debugDateTime);
          String[] dateAndTimeDivision = debugDateTime.split("T");
          // Set the date according to the String given.
          currentDate = dateAndTimeDivision[0];
          // Set the time according to the String given.
          currentTime = convertedDateTime.toGregorianCalendar();
          currentHour = currentTime.get(Calendar.HOUR_OF_DAY);

          //System.out.println("DEBUG: Overriding date: " + currentDate);
          //System.out.println("DEBUG: Overriding hour: " + currentHour);
        }
        catch (Exception e) {
          // Should never happen!
          System.out.println("Bad date format for the debug argument: testing.curr.time!");
        }
      }

      // Get list of flag color codes for the current day
      CarbonFlagSetter flags = new CarbonFlagSetter(currentDate);
      flagColors = flags.getFlags(null);

      //System.out.println("DEBUG: flag colors: " + flagColors);

      color = flagColors.get(currentHour);

      session.setLastHour(currentHour);
      session.setCurrentColor(color);

    }

    // Display appropriate stop light color based on flag code of current hour
    if (color == 0) {
      Label lab = new Label("mylabel");
      lab.add(new SimpleAttributeModifier("id", "stop_light_red"));
      add(lab);
      Label lab2 =
        new Label(
            "leftlabel",
            "Red means the carbon intensity is predicted to be highest during this time.  " +
            "Consumers should avoid using electricty if possible and wait for a lower intensity" +
            " time.");
    lab2.add(new SimpleAttributeModifier("id", "left_bubble"));
    add(lab2);
    }
    else if (color == 1) {
      Label lab = new Label("mylabel");
      lab.add(new SimpleAttributeModifier("id", "stop_light_yellow"));
      add(lab);
      Label lab2 =
        new Label(
            "leftlabel",
            "Yellow means the carbon intensity is predicted to be at a moderate level this time." +
            "  Consumers should avoid using electricty if possible and wait for a lower intensity" +
            " time.");
    lab2.add(new SimpleAttributeModifier("id", "left_bubble"));
    add(lab2);
    }
    else {
      Label lab = new Label("mylabel");
      lab.add(new SimpleAttributeModifier("id", "stop_light_green"));
      add(lab);
      Label lab2 =
        new Label(
            "leftlabel",
            "Green means the carbon intensity is predicted to be lowest during this time.  " +
            "This is a good time for consumers to use electricity.");
    lab2.add(new SimpleAttributeModifier("id", "left_bubble"));
    add(lab2);
    }

    Label lab3 =
        new Label(
            "rightlabel",
            "Consumers should try to plan consumption of electricity during periods of low carbon"
                + " intensity and conserve electricity during moderate and high periods.");
    lab3.add(new SimpleAttributeModifier("id", "right_bubble"));
    add(lab3);

    Label time = new Label("time", "Current Time: " + new Date());
    time.add(new SimpleAttributeModifier("id", "stop_light_time"));
    add(time);
  }

}
